<template>
  <div class="user-manage">
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="id" prop="user_id"> </el-table-column>
      <el-table-column label="姓名" prop="user_name"> </el-table-column>
      <el-table-column label="性别" prop="sex"> </el-table-column>
      <el-table-column label="手机" prop="phone"> </el-table-column>
      <el-table-column label="个性签名" prop="sign"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="config.currentPage"
      :page-sizes="[10, 20]"
      :page-size="config.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 编辑弹框 -->
    <el-dialog title="编辑用户" :visible.sync="dialogFormVisible">
      <el-form :model="userform">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="userform.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="头像" :label-width="formLabelWidth">
          <el-image
            v-if="!userform.avatar.includes('base64')"
            style="width: 100px; height: 100px"
            :src="'http://localhost:3000' + userform.avatar"
          ></el-image>

          <el-image
            v-else
            style="width: 100px; height: 100px"
            :src="userform.avatar"
          ></el-image>

          <!-- 上传的图片组件 -->
          <el-upload
            action="http://localhost:3000/api/admin/upLoadImg"
            :show-file-list="false"
            :auto-upload="false"
            :on-change="handleChange"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="手机" :label-width="formLabelWidth">
          <el-input v-model="userform.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input
            v-model="userform.password"
            autocomplete="off"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-select v-model="userform.sex" placeholder="请选择">
            <el-option label="男" value="男"> </el-option>
            <el-option label="女" value="女"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生日" :label-width="formLabelWidth">
          <el-date-picker
            v-model="userform.birthday"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="个人签名" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            v-model="userform.sign"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// ?currentPage=1&pageSize=8&input=
import { getUserlist, uploadImg, updateUserinfo } from '@/api/user'
export default {
  data () {
    return {
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      formLabelWidth: '120px',
      config: {
        // 当前页码
        currentPage: 1,
        // 每页展示多少条
        pageSize: 10,
        input: ''
      },
      // 保存用户上传的file文件对象
      file: null,
      userform: {
        avatar: '',
        birthday: '',
        password: '',
        phone: '',
        sex: '',
        sign: '',
        userId: null,
        userName: ''
      },
      // 是否改变了图片
      isChangeImg: false
    }
  },
  created () {
    this.requestUserList()
  },

  methods: {
    async requestUserList () {
      const res = await getUserlist(this.config)
      console.log(res)
      this.tableData = res.data.data
      // 总数
      this.total = res.data.total
    },
    handleEdit (row) {
      // 编辑
      // 让弹框显示
      this.dialogFormVisible = true
      console.log(row)
      // 将点击的那一行数据放到userinfo中
      this.userform = { ...row, userId: row.user_id, userName: row.user_name }
    },
    handleDelete (index, row) {
      // 删除
      console.log(index, row)
    },
    handleSizeChange (val) {
      // 修改每页多少条，然后重新发请求
      this.config.pageSize = val
      this.requestUserList()
    },
    handleCurrentChange (val) {
      // 修改当前页码，然后重新发请求
      this.config.currentPage = val
      this.requestUserList()
    },
    handleChange (file, fileList) {
      this.isChangeImg = true
      // console.log(file, fileList)
      const reader = new FileReader()
      // 传入一个参数对象即可得到基于该参数对象的文本内容
      this.file = file.raw
      reader.readAsDataURL(file.raw)
      const that = this
      reader.onload = function (e) {
        // target.result 该属性表示目标对象的DataURL
        that.userform.avatar = e.target.result
      }
    },
    // 处理提交
    async handleSubmit () {
      // 只有改变了图片才执行上传图片
      if (this.isChangeImg) {
        // 上传图片
        const formdata = new FormData()
        formdata.append('file', this.file)
        const res = await uploadImg(formdata)
        // 如果图片上传成功，就上传的图片和用户进行关联
        if (res.data.success_code === 200) {
        // updateUserinfo()
          this.userform.avatar = '/images/avatar/' + res.data.data[0].filename
          const res2 = await updateUserinfo(this.userform)
          console.log(res2)
          // 最后更新用户信息以后需要重置isChangeImg
          this.isChangeImg = false
        } else {
          this.$message('上传失败')
        }
      } else {
        const res3 = await updateUserinfo(this.userform)
        console.log(res3, '❤️')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.user-manange {
  /deep/ .el-dialog {
    height: 500px;
    overflow: auto;
  }
}
</style>
